package race

import "go-study/src/utils"

/**
 * @Description: 2357. 使数组中所有元素都等于零 简单
 * @Keyword:
 * @Author: kami
 * @Date: 2022/8/2 13:46
 **/
func minimumOperations(nums []int) int {
	var exist = make(map[int]bool)

	for i := 0; i < len(nums); i++ {
		if nums[i] != 0 {

			exist[nums[i]] = true
		}
	}
	return len(exist)
}

/**
 * @Description: 2358. 分组的最大数量  中等
 * @Author: kami
 * @Date: 2022/8/2 14:10
 **/
func maximumGroups(grades []int) int {
	var n = len(grades)

	var cnt int
	var base = 0
	for n > 0 {
		base++
		n -= base
		if n > 0 {
			cnt++
		}
	}
	return cnt
}

/**
 * @Description: 2359. 找到离给定两个节点最近的节点 中等
 * @Author: kami
 * @Date: 2022/8/2 14:18
 **/
func closestMeetingNode(edges []int, node1, node2 int) int {
	n := len(edges)
	calcDis := func(x int) []int {
		dis := make([]int, n)
		for i := range dis {
			dis[i] = n
		}
		for d := 0; x >= 0 && dis[x] == n; x = edges[x] {
			dis[x] = d
			d++
		}
		return dis
	}

	d1 := calcDis(node1)
	d2 := calcDis(node2)
	minDis, ans := n, -1
	for i, d := range d1 {
		if d2[i] > d {
			d = d2[i]
		}
		if d < minDis {
			minDis, ans = d, i
		}
	}
	return ans
}

/**
 * @Description: 2360. 图中的最长环 困难
 * @Keyword:
 * @Author: kami
 * @Date: 2022/8/2 14:51
 **/
func longestCycle(edges []int) int {
	var n = len(edges)
	var time = make([]int, n)
	var clock = 1
	var ans = -1
	for i := 0; i < n; i++ {
		// 已访问过
		if time[i] > 0 {
			continue
		}
		var start = clock
		var x = i
		for x != -1 {
			if time[x] > 0 {
				if time[x] >= start {
					ans = utils.GetMax(ans, clock-time[x])
				}
				break
			}
			time[x] = clock
			clock++
			x = edges[x]
		}
	}
	return ans
}
